//
//  SoftInstructionViewController.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/13.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import SnapKit
import WebKit

///使用说明
class SoftInstructionViewController: ViewController, LeftBarButtonItemable {
    var webview = WKWebView.init()
    var content: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        bindUI()
    }
    
    func initUI() {
        setLeftItem()
        
        view.addSubview(webview)
        webview.snp.makeConstraints { (maker) in
            maker.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0))
        }
    }
    
    func bindUI() {
        startAnimating()
        shurenProvider.rx
            .request(.softInstruction)
            .asObservable()
            .mapRespnseJson()
            .subscribe(onNext: { [weak self] (data) in
                guard let json = data as? [String: Any] else { return }
                self?.content = json["content"] as? String
                self?.title = json["title"] as? String
                }, onError: { (error) in
                    stopAnimating()
                    showError(error)
            }, onCompleted: { [weak self] in
                stopAnimating()
                self?.loadweb()
            }).disposed(by: disposeBag)
    }
    
    func loadweb() {
        DispatchQueue.main.async { [weak self] in
            if let content = self?.content {
                self?.webview.loadHTMLString(content, baseURL: nil)
            }
            else {
                
            }
        }
    }
}
